package com.example.netty;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

//ByteToMessageDecoder is an implementation of ChannelInboundHandler 
//which makes it easy to deal with the fragmentation issue.
public class TimeDecoder extends ByteToMessageDecoder {

	@Override
	// ByteToMessageDecoder calls the decode() method with an internally
	// maintained cumulative buffer whenever new data is received.
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		// TODO Auto-generated method stub
		if (in.readableBytes() < 4) {
			// decode() can decide to add nothing to out where there is not
			// enough data in the cumulative buffer. ByteToMessageDecoder will
			// call decode() again when there is more data received.
			System.out.println("没有足够的信息，返回继续读取");
			return;
		}

		// If decode() adds an object to out, it means the decoder decoded a
		// message successfully. ByteToMessageDecoder will discard the read part
		// of the cumulative buffer. Please remember that you don't need to
		// decode multiple messages. ByteToMessageDecoder will keep calling the
		// decode() method until it adds nothing to out.
		out.add(in.readBytes(4));
		System.out.println("取得足够的信息，结束Decode");
	}

}
